package com.oolee.cloud.controller;


import com.oolee.cloud.entity.Payment;
import com.oolee.cloud.general.GeneralResult;
import com.oolee.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/create")
    public GeneralResult<Integer> create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("*******插入结果："+result);
        return result>0 ?
                new GeneralResult<>(1002,"插入payment成功,port: "+serverPort,result):
                new GeneralResult<>(1444,"插入payment失败",null);
    }

    @GetMapping(value = "/payment/get/{id}")
    public GeneralResult<Payment> getPaymentById(@PathVariable("id")Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*******返回结果："+payment+"********");
        return payment != null ?
                new GeneralResult<>(1002,"查询payment成功,port: "+serverPort,payment):
                new GeneralResult<>(1444,"没有对应记录,查询id："+id,null);
    }

    @GetMapping(value = "/payment/timeout")
    public String TimeOutShow(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "timeout 3s";
    }
}
    